package com.hake.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hake.core.Constants;
import com.hake.core.util.WebUtil;

/**
 * 设置公共参数 Filter
 * 1、BaseURL---->http://localhost:8080
 * 2、ctx---->request.contextPath
 * 3、currentURL---->当前地址
 * <p>User: 杨浩泉
 * <p>Date: 2015年9月14日
 * <p>Version: 1.0
 */
public class BaseUrlFilter implements Filter {

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		
		
		if (request.getAttribute(Constants.CONTEXT_PATH) == null) {
            request.setAttribute(Constants.CONTEXT_PATH, request.getContextPath());
        }
        if (request.getAttribute(Constants.BASE_URL) == null) {
            request.setAttribute(Constants.BASE_URL, WebUtil.getBasePath(request));
        }
        
        /* 与MaliciousRequestInterceptor的Session冲突
        if (request.getAttribute(Constants.PREREQUEST) == null) {
        	request.setAttribute(Constants.PREREQUEST, WebUtil.getBackURL(request));
        }
        */
        if (request.getAttribute(Constants.HOST) == null) {
            request.setAttribute(Constants.HOST, WebUtil.getHost(request));
        }
		
        chain.doFilter(request, response);
        return;
	}
    
    
    @Override
	public void init(FilterConfig filterConfig) throws ServletException {}

	@Override
	public void destroy() {}

}
